<template>
  <div class="chartBox">
    <div class="title">{{ title }}</div>
    <div class="drawBox">
      <div class="centerData">
        <p v-if="centerData.indexOf('%') > -1">{{ centerData }}</p>
        <p v-else>{{ centerData | splitNumber }}</p>
        <p>{{ centerDesc }}</p>
      </div>
      <div :id="id" class="chart"></div>
    </div>
    <div class="statistics">
      <p class="desc">
        <span class="dot" style="background: #6ffdb5"></span
        >{{ chartData[0].name }}：{{
          (chartData[0] ? chartData[0].value : '0') | splitNumber
        }}
      </p>
      <p class="desc">
        <span class="dot" style="background: #ffb325"></span
        >{{ chartData[1].name }}：{{
          (chartData[0] ? chartData[1].value : '0') | splitNumber
        }}
      </p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'realnameTotal',
  props: {
    id: {
      type: String,
      default: '',
    },
    title: {
      type: String,
      default: '',
    },
    desc: {
      type: String,
      default: '',
    },
    centerDesc: {
      type: String,
      default: '',
    },
    chartData: {
      type: Array,
      default() {
        return []
      },
    },
  },
  data() {
    return {
      centerData: '',
    }
  },
  watch: {
    chartData: {
      handler(newVal) {
        this.centerData = newVal[2].centerData + ''
        const data = newVal.filter((v, i) => i != 2)
        this.draw(this.id, data)
      },
      deep: true,
    },
  },
  methods: {
    draw(id, data) {
      this.chart = this.$echarts.init(document.getElementById(id))
      var option = {
        tooltip: {
          trigger: 'item',
        },
        color: ['#6FFDB5', '#FFB325'],
        series: [
          {
            name: this.title,
            type: 'pie',
            radius: ['60%', '80%'],
            center: ['50%', '50%'],
            label: {
              normal: {
                position: 'inner',
                show: false,
              },
            },
            data,
          },
        ],
      }
      this.chart.setOption(option)
      window.addEventListener('resize', () => {
        this.chart.resize()
      })
    },
  },
}
</script>

<style lang="less" scoped>
.chartBox {
  width: 100%;
  height: 100%;
  padding: 10px 0;
  .title {
    text-align: center;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #00ffde;
  }
  .drawBox {
    width: 70%;
    height: calc(100% - 50px);
    margin-left: 15%;
    position: relative;
    .chart {
      height: 100%;
    }
    .centerData {
      text-align: center;
      font-size: 12px;
      color: #00ffde;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
  .statistics {
    text-align: left;
    font-size: 12px;
    line-height: 16px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #ffffff;
    padding-left: 20%;
    .desc {
      position: relative;
      padding-left: 20px;
      .dot {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        position: absolute;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
      }
    }
    .num {
      padding-left: 20px;
    }
  }
}
</style>
